package com.zjmzxfzhl.modules.app.controller;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;

import javax.validation.Valid;

import com.zjmzxfzhl.common.core.util.SecurityUtils;
import com.zjmzxfzhl.modules.app.entity.AppDir;
import com.zjmzxfzhl.modules.app.entity.AppTask;
import com.zjmzxfzhl.modules.app.form.AppDocVo;
import com.zjmzxfzhl.modules.app.form.AppProdVo;
import com.zjmzxfzhl.modules.app.form.ProjSumVo;
import liquibase.pro.packaged.O;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zjmzxfzhl.common.core.Result;
import com.zjmzxfzhl.common.core.base.BaseController;
import com.zjmzxfzhl.modules.app.entity.AppProd;
import com.zjmzxfzhl.modules.app.service.AppProdService;

/**
 * Controller
 * 
 * @author 张森荣
 */
@RestController
@RequestMapping("/app/prod")
public class AppProdController extends BaseController {
    @Autowired
    private AppProdService appProdService;

    /**
     * 自定义查询列表
     * 
     * @param appProdVo
     * @param current
     * @param size
     * @param isMaster 查看我负责的产品（0:全部产品,1:我负责的产品）
     * @return
     */
//    @PreAuthorize("@elp.single('app:prod:list')")
    @GetMapping(value = "/list")
    public Result list(AppProdVo appProdVo, @RequestParam Integer current, @RequestParam Integer size,Integer isMaster) {
        String userId = SecurityUtils.getUserId();
        if (isMaster != null && isMaster == 1) {
            appProdVo.setMasterUserId(userId);
        }
        //查看我参与的产品
        if (!"admin".equals(userId)) {
            appProdVo.setMyProd(userId);
        }

        IPage<AppProdVo> pageList = appProdService.list(new Page<AppProdVo>(current, size), appProdVo);
        return Result.ok(pageList);
    }

//    @PreAuthorize("@elp.single('app:prod:list')")
    @GetMapping(value = "/queryById")
    public Result queryById(@RequestParam String id) {
        AppProdVo appProdVo = appProdService.queryById(id);
        if(appProdVo==null){
            return Result.ok();
        }
        return Result.ok(appProdVo);
    }

    /**
     * @功能：新增
     * @param appProd
     * @return
     */
//    @PreAuthorize("@elp.single('app:prod:save')")
    @PostMapping(value = "/save")
    public Result save(@Valid @RequestBody AppProd appProd) {
        Boolean bool =  appProdService.saveAppProd(appProd);
        if (bool == false) {
            return Result.error("添加失败");
        }
        AppProdVo appProdVo = appProdService.queryById(appProd.getProdId());
        return Result.ok(appProdVo);
    }

    /**
     * @功能：修改
     * @param appProd
     * @return
     */
//    @PreAuthorize("@elp.single('app:prod:update')")
    @PutMapping(value = "/update")
    public Result update(@Valid @RequestBody AppProd appProd) {
        Boolean result=appProdService.updateAppProd(appProd);
        if(result==false){
            return Result.error("修改失败！");
        }
        return Result.ok();
    }

    /**
     * @功能：批量删除
     * @param ids
     * @return
     */
//    @PreAuthorize("@elp.single('app:prod:delete')")
    @DeleteMapping(value = "/delete")
    public Result delete(@RequestParam String ids) {
        if (ids == null || ids.trim().length() == 0) {
            return Result.error("ids can't be empty");
        }
        //返回删除对象
        String[] idsArr = ids.split(",");
        List<AppProd> appProdList=new ArrayList<>();
        for (String s:idsArr) {
            AppProd appProd=appProdService.getById(s);
            appProdList.add(appProd);
        }
        Boolean result=appProdService.delByIds(ids);
        if(result==false){
            return Result.error("删除失败！");
        }
        return Result.ok(appProdList);
    }

    /**
     * @功能： 产品变更{00.导入期,01.成长期,02.成熟期,03.哀退期,04.已关闭}
     * @param prodId
     * @param status
     * @return
     */
//    @PreAuthorize("@elp.single('app:Doc:update')")
    @PutMapping(value = "/prodChange")
    public Result prodChange(@RequestParam String prodId,@RequestParam String status) {
        Boolean result =true;
        switch (status) {
            case "00": //导入期
                result =appProdService.introduction(prodId);
                break;
            case "01": //成长期
                result =appProdService.anagen(prodId);
                break;
            case "02": //成熟期
                result =appProdService.maturation(prodId);
                break;
            case "03": //哀退期
                result =appProdService.catagen(prodId);
                break;
            case "04": //已关闭
                result =appProdService.close(prodId);
                break;
            default: return Result.error("查找文档状态失败！");
        }
        if(result==false){
            return Result.error("文档状态更改失败！");
        }
        return Result.ok();
    }

    /**
     * 统计产品概括
     *
     * @return
     */
    //    @PreAuthorize("@elp.single('app:proj:list')")
    @GetMapping(value = "/summarize")
    public Result summarize(String prodId) {
        Map<String, Object> result = appProdService.summarize(prodId);
        return Result.ok(result);
    }
}
